#!/bin/bash
#
#	Automates the process of generating a panorama from indiviual photos
#
#	requires:	sudo aptitude install autopano-sift hugin-tools enblend

DISPLAY=:0

if [ -z "$1" ]
then
	echo "Usage: `basename $0` <horizontal_fov> <image1> <image2> ... "
	exit 1
fi  

hfov=$1
printf "Horizontal field of view: "$hfov"\n"
shift

no_images="$#"
images="$*"

# echo $images

echo
echo "Generating key points..."
COUNTER=1
while [ ! $COUNTER -gt $no_images ]; do
	im[$COUNTER]=$1
	echo -e "\t"${im[$COUNTER]}"  --->  "${im[$COUNTER]}.key.gz
	generatekeys ${im[$COUNTER]} ${im[$COUNTER]}.key.gz 480 &> /dev/null
	let COUNTER=COUNTER+1
	shift
done

echo
echo "Matching key points..."
autopano pano.pto *.key.gz  &> /dev/null
echo -e "\t returned: "$?

echo
echo "Optimizing (with horizontal field of view: "$hfov")..."
autooptimiser -v60 -a -l -p -s -o pano_opt.pto pano.pto  &> /dev/null
echo -e "\t returned: "$?

pto2mk -o pano.pto.mk -p pano pano_opt.pto  &> /dev/null

echo
echo "Stitching and blending..."
make -f pano.pto.mk all clean  &> /dev/null
echo -e "\t returned: "$?

PTcrop -f -p "" pano.tif &> /dev/null
convert pano.tif pano.jpeg &> /dev/null


rm *.pto*
rm *.key.gz
rm *.tif

echo
echo "Done!"

exit 0
